package array;

public class Array<E> {

    private E[] data;

    private int size;

    public Array(int capacity){
        data = (E[])new Object[capacity];
        size = 0;
    }

    public Array(){
        this(10);
    }

    public Array(E[] arr){
       data = (E[])new Object[arr.length];
        for (int i = 0; i < arr.length; i++) {
            data[i] = arr[i];
            size++;
        }
    }
    //获取元素个数
    public int getSize(){
        return size;
    }
    //获取数组容量
    public int getCapacity(){
        return data.length;
    }

    public boolean isEmpty(){
        return size == 0;
    }

    //往最后一个加元素就是 往索引为 size 处加元素，再把size++
    public void addLast(E e){
        add(size,e);
    }
    //往索引为 index 处加元素，再把size++
    public void add(int index,E e){

        if(index<0||index>size)
            throw new IllegalArgumentException("add failed,because index<0||index>size ");

        if (size==data.length) resize();

        for (int i = size - 1;i>=index; i--) {
            data[i+1] = data[i];
        }
        data[index] = e;
        size++;
    }

    private void resize(){
       E[]  newData = (E[])new Object[data.length << 2];
        for (int i = 0; i < size; i++) {
            newData[i] = data[i];
        }
        data = newData;
    }

    //获取index上的元素
    public E get(int index){
        if(index<0 || index>size){
            throw new IllegalArgumentException("add failed,because index<0||index>size ");
        }
        return data[index];
    }

    public E getFirst(){
        return get(0);
    }

    public E getLast(){
        return get(size-1);
    }

    //改变索引上的值
    public void set(int index,E e){
        if(index<0 || index>size){
            throw new IllegalArgumentException("add failed,because index<0||index>size ");
        }
        data[index] = e;
    }

    //通过值找到索引的位置
    public int find(E e){
        for (int i = 0; i < data.length; i++) {
            if (data[i].equals(e)) {
                return i;
            }
        }
        return -1;
    }

    //删除指定位置上的元素，并将元素返回
    public E remove(int index){
        if(index<0 || index>size){
            throw new IllegalArgumentException("add failed,because index<0||index>size ");
        }
        E result = data[index];
        for (int i = index; i < size - 1 ; i++) {
            data[i] = data[i+1];
        }
        //让最后一个元素进行回收
        data[size-1] = null;
        size--;
        return result;
    }

    //删除最后一个元素
    public E removeLast(){
        return remove(size-1);
    }

    //删除最后一个元素
    public E removeFirst(){
        return remove(0);
    }

    //删除指定元素
    public void removeElement(E e){
        int result = find(e);
        if (result == -1) {
            remove(result);
        }
    }

    //查看数组中是否存在某个元素
    public boolean contains(E e){
        for (int i = 0; i < size; i++) {
            if(data[i].equals(e)) return true;
        }
        return false;
    }

    //交换位置
    public void swap(int i,int j){
        if (i< 0||i>size||j<0||j>size) {
            throw new RuntimeException("数组越界");
        }
       E temp = data[i];
       data[i] = data[j];
       data[j] = temp;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Data capacity:"+data.length+"size:"+size+",data:[");
        for (int i = 0; i < size; i++) {
            sb.append(data[i]);
            if(i != size -1){
                sb.append(",");
            }
        }
        return sb.append("]").toString();
    }

    public static void main(String[] args) {
        MyArray myArray = new MyArray(10);
        for (int i = 0; i < 5; i++) {
            myArray.addLast(i);
        }
        System.out.println(myArray);
        myArray.add(1,100);
        System.out.println(myArray);
        myArray.addLast(100);
        System.out.println(myArray);
        myArray.removeLast();
        System.out.println(myArray);
        myArray.remove(1);
        System.out.println(myArray);
    }
}
